﻿using CommonBase;
using CommonBase.ResponseBase;
using CustomControls.PoptipCtr.Base;
using CustomControls.PoptipCtr;
using LogProjectManager;
using ManagementToolClient.BaseViewModels;
using ManagementToolClient.Models;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Ioc;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using ManagementToolClient.PageDeclaration;
using System.Windows;
using Prism.Regions;

namespace ManagementToolClient.ViewModels
{
    public class CompanyManagerPageViewModel : BaseViewModel
    {
        private Dictionary<int, int> CurrentItemIndex { set; get; }


        private ObservableCollection<CompanyManagerModel> _CompanyList = new ObservableCollection<CompanyManagerModel>();
        public ObservableCollection<CompanyManagerModel> CompanyList
        {
            get { return _CompanyList; }
            set { SetProperty(ref _CompanyList, value); }
        }

        private DelegateCommand _AddCompanyCommand;
        public DelegateCommand AddCompanyCommand =>
            _AddCompanyCommand ?? (_AddCompanyCommand = new DelegateCommand(ExcuteAddCompanyCommand));


        private DelegateCommand<CompanyManagerModel> _EditCompanyCommand;
        public DelegateCommand<CompanyManagerModel> EditCompanyCommand =>
            _EditCompanyCommand ?? (_EditCompanyCommand = new DelegateCommand<CompanyManagerModel>(ExcuteEditCompanyCommand));

        private DelegateCommand<CompanyManagerModel> _DeletetCompanyCommand;
        public DelegateCommand<CompanyManagerModel> DeletetCompanyCommand =>
            _DeletetCompanyCommand ?? (_DeletetCompanyCommand = new DelegateCommand<CompanyManagerModel>(ExcuteDeletetCompanyCommand));

        private DelegateCommand<CompanyManagerModel> _AddDveCompanyCommand;
        public DelegateCommand<CompanyManagerModel> AddDveCompanyCommand =>
            _AddDveCompanyCommand ?? (_AddDveCompanyCommand = new DelegateCommand<CompanyManagerModel>(ExcuteAddDveCompanyCommand));

        private DelegateCommand<CompanyManagerModel> _SearchCompanyCommand;
        public DelegateCommand<CompanyManagerModel> SearchCompanyCommand =>
            _SearchCompanyCommand ?? (_SearchCompanyCommand = new DelegateCommand<CompanyManagerModel>(ExcuteSearchCompanyCommand));

   

        public CompanyManagerPageViewModel(IContainerExtension container) : base(container)
        {

        }

        public override void ExecuteLoadedCommand()
        {
            base.ExecuteLoadedCommand();
            GetDate();
        }

        public override void ExecuteUnLoadedCommand()
        {
            base.ExecuteUnLoadedCommand();
        }

        private void ExcuteAddCompanyCommand()
        {
            LayDialogParameter _dialogParameter = new LayDialogParameter();

            LayDialogControl.ShowDialog(SystemResourceExten.Nav_AddCompanyWindow, _dialogParameter, rest =>
            {
                switch (rest.Result)
                {
                    case ButtonResult.Yes:
                        GetDate();
                        break;
                    case ButtonResult.No:

                        break;
                    case ButtonResult.Default:
                        break;
                    default:
                        break;
                }
            }, SystemResource.Nav_RootDialogTooken);
        }

        private void ExcuteEditCompanyCommand(CompanyManagerModel _CompanyManagerItem)
        {

        }

        private void ExcuteDeletetCompanyCommand(CompanyManagerModel _CompanyManagerItem)
        {

        }

        private void ExcuteAddDveCompanyCommand(CompanyManagerModel _CompanyManagerItem)
        {
            LayDialogParameter _dialogParameter = new LayDialogParameter();
            _dialogParameter.Add("UUID", _CompanyManagerItem.uuid);
            LayDialogControl.ShowDialog(SystemResourceExten.Nav_AddDevicePage, _dialogParameter, rest =>
            {
                switch (rest.Result)
                {
                    case ButtonResult.Yes:
                        MessageControl.Warning("添加成功!", "RootMessageTooken");
                        break;
                    case ButtonResult.No:

                        break;
                    case ButtonResult.Default:
                        break;
                    default:
                        break;
                }
            }, SystemResource.Nav_RootDialogTooken);
        }
        private void ExcuteSearchCompanyCommand(CompanyManagerModel _CompanyManagerItem)
        {
            NavigationParameters itemParameter = new NavigationParameters();
            itemParameter.Add("companyid", _CompanyManagerItem.uuid);
            Application.Current?.Dispatcher.Invoke(() => {
                Region.RequestNavigate(SystemResourceExten.Nav_MainContent, SystemResourceExten.Nav_DeviceManagerView, itemParameter);
            });
        }
        private void GetDate() 
        {
            CompanyList.Clear();
            Task.Run(async () => {
                try
                {
                    var url = "http://127.0.0.1:8081" + "/api/GetCompanyByPage?" + $"inputpageCode=1" + "&" + $"inputPageSize=100";

                    var httpClient = new System.Net.Http.HttpClient();


                    var response = httpClient.GetAsync(url).Result;
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        string responseBody = await response.Content.ReadAsStringAsync();
                        var _itemResult = JsonConvert.DeserializeObject<AuthorizationCodeResponseBody>(responseBody);

                        if (_itemResult.code == ((int)ErrorCode.OK).ToString()) 
                        {
                            if (_itemResult.Data != null)
                            {
                                var itemList = JsonConvert.DeserializeObject<List<CompanyManagerModel>>(_itemResult.Data.ToString());
                                if (itemList.Count>0) 
                                {
                                    foreach (var item in itemList) 
                                    {
                                        Application.Current.Dispatcher.Invoke(() => {
                                            CompanyList.Add(item);
                                        });
                                    }
                                }
                            }
                        }
                      
                        else 
                        {
                            LogManager.WriteLog(LogTypeEnum.Normal, "ToCheckVersionAndUpdate", "访问接口异常,data为空");
                        }
                       
                    }
                    else
                    {
                        LogManager.WriteLog(LogTypeEnum.Exception, "ToCheckVersionAndUpdate", "接口访问失败");
                    }
                }
                catch (Exception ex)
                {
                    LogManager.WriteLog(LogTypeEnum.Exception, "GetDate", ex.Message);
                }
            });
        }
    
    }
}
